package com.czl.module_activity.viewmodel

import androidx.databinding.ObservableField
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.CommunityBean
import com.czl.base.data.bean.CommunityRequestBean
import com.czl.base.data.bean.CommunityResponseBean
import com.czl.base.data.bean.NoticeResponseBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.util.RxThreadHelper

class BulletinListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model){

    var currentPage = 0
    val uc = UiChangeEvent()
    val notice= ObservableField<NoticeResponseBean.Record>()
    var showTitle=ObservableField<Boolean>(true)
    var showRv=ObservableField<Boolean>(true)
    class UiChangeEvent {
        val refreshListEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val loadCompleteEvent: SingleLiveEvent<NoticeResponseBean> = SingleLiveEvent()
        val toBulletinDetailEvent: SingleLiveEvent<Int> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getBullletin()
    })

    val onFirstBulletinClick: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.toBulletinDetailEvent.postValue(notice.get()!!.id)
    })

    private fun getBullletin() {

        model.getNotices(CommunityRequestBean(model.getAreaId(),"","",currentPage+1,10))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<NoticeResponseBean>>(){
                override fun onResult(t: BaseBean<NoticeResponseBean>) {
                    if (t.code == 200) {
                        currentPage++
//                        var nbs= mutableListOf<NoticeResponseBean.Record>()
//                        for (i in 1..30){
//                            nbs.add(NoticeResponseBean.Record("2022-01-24 16:59:07",i,0, "停电$i",2))
//                        }
//                        var nb=NoticeResponseBean("",1,100,true, listOf(),1, nbs,true,30,30)
                        if (t.data!!.current==1){
                            notice.set(null)
                            if (t.data!!.records.isNotEmpty()&& t.data?.records?.size!! > 0){
                                notice.set((t.data!!.records as MutableList<*>).removeAt(0) as NoticeResponseBean.Record?)
                            }
                        }
                        if (t.data!!.records.size==0){
                            showTitle.set(false)
                        }else{
                            showTitle.set(true)
                        }
                        if (t.data!!.records.size==0&&notice.get()!=null){
                            showRv.set(false)
                        }else{
                            showRv.set(true)
                        }
//                        if (t.data?.records?.size!! > 0){
                            uc.loadCompleteEvent.postValue(t.data)
//                        }else{
//                            uc.loadCompleteEvent.postValue(null)
//                        }
//                        uc.loadCompleteEvent.postValue(nb)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }


    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 0
        getBullletin()
    })
}